package com.jcfk.eam.controller.bd;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.business.utils.DataBindHelper;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import com.jcfk.eam.domain.dto.base.EamAssetPositionDTO;
import com.jcfk.eam.domain.query.base.EamAssetPositionQuery;
import com.jcfk.eam.enums.AssetKind;
import com.jcfk.eam.service.base.EamAssetPositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import java.util.List;

/**
 * 资产组成部位
 *
 * @author yrl
 * @email 
 * @date 2024-12-04
 */
@Api(tags = {"资产组成部位管理"})
@RestController
@RequestMapping("eamassetposition")
public class EamAssetPositionController {
    @Autowired
    private EamAssetPositionService eamAssetPositionService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamAssetPositionQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询资产组成部位")
    @PostMapping("/queryByPage")
    public RetPage<EamAssetPositionDTO> queryByPage(@RequestBody ReqPageParam<EamAssetPositionQuery> requestParam) {
        DataPage<EamAssetPositionDTO> page = new DataPage<EamAssetPositionDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamAssetPositionDTO.class);
        page = eamAssetPositionService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 获取资产组成部位树
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "EamAssetTypeQuery", name = "ids", value = "", required = true)})
    @ApiOperation(value = "获取设备类型树", notes = "获取设备类型树", httpMethod = "POST")
    @PostMapping("getTree")
    public RetResult<List<EamAssetPositionDTO>> getTree(@RequestBody ReqPageParam<EamAssetPositionQuery> query) {
        return RetResult.ok(eamAssetPositionService.getTree(query.getData()));
    }

    /**
     *  资产组成部位
     * 根据id 查询详情
     */
    @ApiOperation("查询资产组成部位")
    @GetMapping("/get/{id}")
    public RetResult<EamAssetPositionDTO> get(@PathVariable("id") String id) {
            EamAssetPositionDTO item = eamAssetPositionService.get(id);

        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理资产组成部位
     */
    @ApiOperation("保存资产组成部位")
    @PostMapping("/save")
    @RequestLogHeader(modular = "资产组成部位管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamAssetPositionDTO eamAssetPosition) {
        // 数据校验
        RetResult ret = eamAssetPositionService.isValid(eamAssetPosition);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamAssetPosition.getId())){
            // 新增
            eamAssetPositionService.insert(eamAssetPosition);
        } else{
            // 更新
            eamAssetPositionService.update(eamAssetPosition);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除资产组成部位
     */
    @ApiOperation("批量删除资产组成部位")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "资产组成部位管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamAssetPositionDTO eamAssetPosition) {
        return RetResult.ok(eamAssetPositionService.logicDeleteList(eamAssetPosition));
    }





    /**
     *  删除资产组成部位
     */
    @ApiOperation("删除资产组成部位")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "资产组成部位管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamAssetPositionService.deleteItem(id);
        return RetResult.ok();
    }
}
